package ru.urfc.portal.ui.client.googlets.client;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.controllers.GridController;
import ru.urfc.portal.ui.client.controllers.events.GetEvent;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.descriptor.GridField;
import ru.urfc.portal.ui.client.model.catalogs.DocumentState;
import ru.urfc.portal.ui.client.renderers.UrlRenderer;
import ru.urfc.portal.ui.client.widgets.DocumentFilter;
import ru.urfc.portal.ui.client.widgets.MegaGrid;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;

public class DocumentsList extends BaseGooglet {

	interface Binder extends UiBinder<Widget, DocumentsList> {}
	private static Binder binder = GWT.create(Binder.class);

	@UiField DocumentFilter documentFilter;
	@UiField MegaGrid grid;
	@UiField Button search;
	
	public Type<GridController> SEARCH = new Type<GridController>();

	@UiHandler("search")
	void search(ClickEvent e) {
		getEventsDispatcher().fireEvent(new GetEvent(SEARCH));
	}
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		documentFilter.setupButton(search.getElement());
		documentFilter.init(this);

		GridDescriptor descriptor = new GridDescriptor(DOCUMENT, CLIENTS, getClientId(), DOCUMENTS);
		GridField autoExpandColumnField = 
			descriptor.addAnchorColumn("type/text", "id", PAGE_DOCUMENT_DETAILS, "Наименование", 200);
		GridField typeCodeField = descriptor.addField("type/code");
		autoExpandColumnField.<UrlRenderer>getRenderer().addField(typeCodeField, "{typeCode}");
		descriptor.addTextColumn("outgoingNumber", "Исх. №", 100);
		descriptor.addDateColumn("outgoingDate", "Исх. дата", 100);
		descriptor.addCIColumn("state/text", "state/code", DocumentState.class, "Состояние", 150);
		descriptor.addDateColumn("closeDate", "Закрыта", 100);
		
		register(SEARCH, createController(descriptor, grid)).setFilter(documentFilter);

		grid.setAutoExpandColumn(autoExpandColumnField.getName());
	}

}